# 检测字符串，长度，和是否为空
from datetime import datetime


def check_str(value, nullable=True, max_length=None):
    if not value:
        return nullable

    if max_length and len(value) > max_length:
        return False
    return True


# 检测数字，最大值和最小值，否为空
def check_int(value, nullable=True, min=None, max=None):
    if not value:
        return nullable

    try:
        value_int = int(value)

        if min and value_int < min:
            return False
        if max and value_int > max:
            return False
    except Exception as e:
        return False
    return True


# 检测数字，最大值和最小值，否为空
def check_float(value, nullable=True, min=None, max=None):
    if not value:
        return nullable

    try:
        value_float = float(value)

        if min and value_float < min:
            return False
        if max and value_float > max:
            return False
    except Exception as e:
        return False
    return True


# 检测数字，最大值和最小值，否为空
def check_date(value, nullable=True):
    if not value:
        return nullable

    try:
        datetime.strptime(value, '%Y-%m-%d')
    except Exception as e:
        return False
    return True
